package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.AssessmentPaperPersonMapper;
import com.zhijian.medical.entity.po.AssessmentPaperPerson;
import com.zhijian.medical.entity.vo.request.AssessmentPaperPersonSearchVo;
import com.zhijian.medical.entity.vo.response.AssessmentPaperPersonVo;
import com.zhijian.medical.service.AssessmentPaperPersonService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 考核-试卷-人员关系表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-02-02 14:45:42
 */
@Slf4j
@Service
public class AssessmentPaperPersonServiceImpl implements AssessmentPaperPersonService {

    @Autowired
    private AssessmentPaperPersonMapper mapper;

    @Override
    public AssessmentPaperPerson selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AssessmentPaperPersonVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByAssessmentId(Long assessmentId) {
        mapper.deleteByAssessmentId(assessmentId);
    }

    @Override
    public Long insert(AssessmentPaperPerson assessmentPaperPerson) {
        Date now = new Date();
        assessmentPaperPerson.setCreateTime(now);
        assessmentPaperPerson.setUpdateTime(now);
        return mapper.insert(assessmentPaperPerson) > 0 ? assessmentPaperPerson.getId() : 0L;
    }

    @Override
    public boolean update(AssessmentPaperPerson assessmentPaperPerson) {
        Date now = new Date();
        assessmentPaperPerson.setUpdateTime(now);
        return mapper.updateByPrimaryKey(assessmentPaperPerson) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(AssessmentPaperPerson record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean updateByUserIdAndAssessmentId(AssessmentPaperPerson record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByUserIdAndAssessmentId(record);
    }

    @Override
    public Integer count(AssessmentPaperPersonSearchVo assessmentPaperPersonSearchVo) {
        return mapper.count(assessmentPaperPersonSearchVo);
    }

    @Override
    public List<AssessmentPaperPersonVo> selectList(AssessmentPaperPersonSearchVo assessmentPaperPersonSearchVo) {
        return mapper.selectList(assessmentPaperPersonSearchVo);
    }

    @Override
    public AssessmentPaperPersonVo selectByAssessmentPaperId(Long assessmentPaperId) {
        List<AssessmentPaperPersonVo> assessmentPaperPersonVos = mapper.selectList(AssessmentPaperPersonSearchVo.builder()
                .assessmentPaperId(assessmentPaperId)
                .build());
        return CollectionUtil.isNotEmpty(assessmentPaperPersonVos) ? assessmentPaperPersonVos.get(0) : null;
    }

    @Override
    public AssessmentPaperPersonVo selectByAssessmentIdAndPersonId(Long assessmentId, Long personId) {
        return mapper.selectByAssessmentIdAndPersonId(assessmentId, personId);
    }

    @Override
    public Page<AssessmentPaperPersonVo> selectPage(AssessmentPaperPersonSearchVo assessmentPaperPersonSearchVo) {
        //先直接查总数
        int count = count(assessmentPaperPersonSearchVo);
        //分页
        Page<AssessmentPaperPersonVo> page = new Page<>(true, count, assessmentPaperPersonSearchVo.getPageNum(), assessmentPaperPersonSearchVo.getPageSize());
        assessmentPaperPersonSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AssessmentPaperPersonVo> responseVos = this.selectList(assessmentPaperPersonSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<AssessmentPaperPerson> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public BigDecimal selectAvgScoreByAssessmentId(Long assessmentId) {
        return mapper.selectAvgScoreByAssessmentId(assessmentId);
    }

}